package com.baoteng.rbspy.vo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class JsonDateSerializer extends JsonSerializer<Date>{

	@Override
	public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
			throws IOException, JsonProcessingException {
		 if(value != null){
			 Calendar calendar = Calendar.getInstance();
			 calendar.setTime(value);
			if (calendar.get(Calendar.HOUR_OF_DAY) == 0 && calendar.get(Calendar.MINUTE) == 0
					&& calendar.get(Calendar.SECOND) == 0) {
				 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		         String formattedDate = formatter.format(value);
		         gen.writeString(formattedDate);
			}else{
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		         String formattedDate = formatter.format(value);
		         gen.writeString(formattedDate);
			}
			
		 }
	}

}
